using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Module.Common.Models;
using System.Web.UI.WebControls;
using Module.Core.Common;
using Module.Core.EntitiesInfo;

namespace Module.Core.DAO
{
	public class CMSZonesDAO
	{
		#region Fields

		protected string connectionStringName;

		#endregion
        
		#region Methods

		/// <summary>
		/// Saves a record to the CMS_Zones table.
		/// </summary>
		public virtual int Insert(CMSZonesInfo cMSZonesInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@ZoneIDLang", cMSZonesInfo.ZoneIDLang),
				new SqlParameter("@ParentID", cMSZonesInfo.ParentID),
				new SqlParameter("@Title", cMSZonesInfo.Title),
				new SqlParameter("@Priority", cMSZonesInfo.Priority),
				new SqlParameter("@Lang", cMSZonesInfo.Lang),
				new SqlParameter("@CreatedOn", cMSZonesInfo.CreatedOn),
				new SqlParameter("@CreatedBy", cMSZonesInfo.CreatedBy),
				new SqlParameter("@DeletedOn", cMSZonesInfo.DeletedOn),
				new SqlParameter("@DeletedBy", cMSZonesInfo.DeletedBy),
				new SqlParameter("@KeyZone", cMSZonesInfo.KeyZone),
				new SqlParameter("@ZoneUrl", cMSZonesInfo.ZoneUrl),
				new SqlParameter("@ModuleMenu", cMSZonesInfo.ModuleMenu),
				new SqlParameter("@ImagesZone", cMSZonesInfo.ImagesZone),
				new SqlParameter("@StatusClick", cMSZonesInfo.StatusClick),
				new SqlParameter("@CheckZone", cMSZonesInfo.CheckZone),
				new SqlParameter("@TrangHienThi", cMSZonesInfo.TrangHienThi)
			};

            return cMSZonesInfo.ZoneID = (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "CMS_Zones_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the CMS_Zones table.
		/// </summary>
		public virtual void Update(CMSZonesInfo cMSZonesInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@ZoneID", cMSZonesInfo.ZoneID),				
				new SqlParameter("@ParentID", cMSZonesInfo.ParentID),
				new SqlParameter("@Title", cMSZonesInfo.Title),
				new SqlParameter("@Priority", cMSZonesInfo.Priority),
				new SqlParameter("@Lang", cMSZonesInfo.Lang),
				new SqlParameter("@CreatedOn", cMSZonesInfo.CreatedOn),
				new SqlParameter("@CreatedBy", cMSZonesInfo.CreatedBy),
				new SqlParameter("@DeletedOn", cMSZonesInfo.DeletedOn),
				new SqlParameter("@DeletedBy", cMSZonesInfo.DeletedBy),
				new SqlParameter("@KeyZone", cMSZonesInfo.KeyZone),
				new SqlParameter("@ZoneUrl", cMSZonesInfo.ZoneUrl),
				new SqlParameter("@ModuleMenu", cMSZonesInfo.ModuleMenu),
				new SqlParameter("@ImagesZone", cMSZonesInfo.ImagesZone),
				new SqlParameter("@StatusClick", cMSZonesInfo.StatusClick),
				new SqlParameter("@CheckZone", cMSZonesInfo.CheckZone),
				new SqlParameter("@TrangHienThi", cMSZonesInfo.TrangHienThi)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CMS_Zones_Update", parameters);
		}


        public virtual void UpdateZoneUrl(int zoneId, string zoneUrl)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@zoneId", zoneId),				
				new SqlParameter("@zoneUrl", zoneUrl)
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CMS_Zones_UpdateZoneUrl", parameters);
        }

		/// <summary>
		/// Deletes a record from the CMS_Zones table by its primary key.
		/// </summary>
		public virtual void Delete(int zoneID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@ZoneID", zoneID)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CMS_Zones_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the CMS_Zones table.
		/// </summary>
        public virtual CMSZonesInfo Select(int zoneId)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@zoneId", zoneId)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CMS_Zones_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeCMSZonesInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

        public CHRTList<CMSZonesInfo> SelectModule(int? Type, string Para_1, string Para_2)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Type", Type),
                new SqlParameter("@Para_1", Para_1),
                new SqlParameter("@Para_2", Para_2)         
			};
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CMS_Zones_SelectModuleType", parameters))
            {
                CHRTList<CMSZonesInfo> cMSZonesInfoList = new CHRTList<CMSZonesInfo>();
                while (dataReader.Read())
                {
                    CMSZonesInfo cMSZonesInfo = MakeCMSZonesInfo(dataReader);
                    cMSZonesInfoList.Add(cMSZonesInfo);
                }

                return cMSZonesInfoList;
            }
        }        

		/// <summary>
		/// Selects all records from the CMS_Zones table.
		/// </summary>
		public CHRTList<CMSZonesInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CMS_Zones_SelectAll"))
			{
				CHRTList<CMSZonesInfo> cMSZonesInfoList = new CHRTList<CMSZonesInfo>();
				while (dataReader.Read())
				{
					CMSZonesInfo cMSZonesInfo = MakeCMSZonesInfo(dataReader);
					cMSZonesInfoList.Add(cMSZonesInfo);
				}

				return cMSZonesInfoList;
			}
		}

        public CHRTList<CMSZonesInfo> GetParentZones(int parentId, int lang, int roleID, int userID)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@ParentID", parentId),
                new SqlParameter("@Lang", lang),
                new SqlParameter("@RoleID", roleID),
                new SqlParameter("@UserID", userID),                
			};
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CMS_Zones_GetByParent",parameters))
            {
                CHRTList<CMSZonesInfo> cMSZonesInfoList = new CHRTList<CMSZonesInfo>();
                while (dataReader.Read())
                {
                    CMSZonesInfo cMSZonesInfo = MakeCMSZonesInfo(dataReader);
                    cMSZonesInfoList.Add(cMSZonesInfo);
                }

                return cMSZonesInfoList;
            }
        }
		/// <summary>
		/// Creates a new instance of the CMS_Zones class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual CMSZonesInfo MakeCMSZonesInfo(SqlDataReader dataReader)
		{
			CMSZonesInfo cMSZonesInfo = new CMSZonesInfo();
			cMSZonesInfo.ZoneID = SqlClientUtility.GetInt32(dataReader,DbConstants.CMS_ZONES.ZONEID, 0);
			cMSZonesInfo.ZoneIDLang = SqlClientUtility.GetInt32(dataReader,DbConstants.CMS_ZONES.ZONEIDLANG, 0);
			cMSZonesInfo.ParentID = SqlClientUtility.GetInt32(dataReader,DbConstants.CMS_ZONES.PARENTID, 0);
			cMSZonesInfo.Title = SqlClientUtility.GetString(dataReader,DbConstants.CMS_ZONES.TITLE, String.Empty);
			cMSZonesInfo.Priority = SqlClientUtility.GetInt32(dataReader,DbConstants.CMS_ZONES.PRIORITY, 0);
			cMSZonesInfo.Lang = SqlClientUtility.GetInt32(dataReader,DbConstants.CMS_ZONES.LANG, 0);
			cMSZonesInfo.CreatedOn = SqlClientUtility.GetDateTime(dataReader,DbConstants.CMS_ZONES.CREATEDON, DateTime.Now);
			cMSZonesInfo.CreatedBy = SqlClientUtility.GetInt32(dataReader,DbConstants.CMS_ZONES.CREATEDBY, 0);
			cMSZonesInfo.DeletedOn = SqlClientUtility.GetDateTime(dataReader,DbConstants.CMS_ZONES.DELETEDON, DateTime.Now);
			cMSZonesInfo.DeletedBy = SqlClientUtility.GetInt32(dataReader,DbConstants.CMS_ZONES.DELETEDBY, 0);
			cMSZonesInfo.KeyZone = SqlClientUtility.GetString(dataReader,DbConstants.CMS_ZONES.KEYZONE, String.Empty);
			cMSZonesInfo.ZoneUrl = SqlClientUtility.GetString(dataReader,DbConstants.CMS_ZONES.ZONEURL, String.Empty);
			cMSZonesInfo.ModuleMenu = SqlClientUtility.GetInt32(dataReader,DbConstants.CMS_ZONES.MODULEMENU, 0);
			cMSZonesInfo.ImagesZone = SqlClientUtility.GetString(dataReader,DbConstants.CMS_ZONES.IMAGESZONE, String.Empty);
			cMSZonesInfo.StatusClick = SqlClientUtility.GetString(dataReader,DbConstants.CMS_ZONES.STATUSCLICK, String.Empty);
			cMSZonesInfo.CheckZone = SqlClientUtility.GetInt32(dataReader,DbConstants.CMS_ZONES.CHECKZONE, 0);
            cMSZonesInfo.TrangHienThi = SqlClientUtility.GetString(dataReader, DbConstants.CMS_ZONES.TRANGHIENTHI, string.Empty);

			return cMSZonesInfo;
		}

        public virtual void GetZoneTypeLatesXML(string fileNameXML, string pathWriteXML, int lang, int checkZone, int parentID, int moduleMenu, string keyOrderby)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{				
                new SqlParameter("@CheckZone", checkZone),
                new SqlParameter("@ParentID", parentID),
                new SqlParameter("@ModuleMenu", moduleMenu),
                new SqlParameter("@KeyOrderby", keyOrderby)                
			};
            using (DataSet ds = SqlClientUtility.ExecuteDataSet(connectionStringName, CommandType.StoredProcedure, "CMS_Zones_GetSite", parameters))
            {
                if (ds.Tables[0].Rows.Count > 0)
                    ds.WriteXml(pathWriteXML + "\\" + fileNameXML + ".xml");
                else

                    ComomFunctions.DeleteFile(pathWriteXML + "\\" + fileNameXML + ".xml");
            }
        }

        public DataTable GetMenu(int checkZone, int parentID, int moduleMenu, string keyOrderby)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{				
                new SqlParameter("@CheckZone", checkZone),
                new SqlParameter("@ParentID", parentID),
                new SqlParameter("@ModuleMenu", moduleMenu),
                new SqlParameter("@KeyOrderby", keyOrderby)                
			};
            return SqlClientUtility.ExecuteDataTable(connectionStringName, CommandType.StoredProcedure, "CMS_Zones_GetSite", parameters);
        }
        protected virtual CMSZonesInfo GetCMSZonesInfo(SqlDataReader dataReader)
        {
            CMSZonesInfo cMSZonesInfo = new CMSZonesInfo();
            cMSZonesInfo.ZoneID = SqlClientUtility.GetInt32(dataReader, DbConstants.CMS_ZONES.ZONEID, 0);
            cMSZonesInfo.ZoneIDLang = SqlClientUtility.GetInt32(dataReader, DbConstants.CMS_ZONES.ZONEIDLANG, 0);
            cMSZonesInfo.ParentID = SqlClientUtility.GetInt32(dataReader, DbConstants.CMS_ZONES.PARENTID, 0);
            cMSZonesInfo.Title = SqlClientUtility.GetString(dataReader, DbConstants.CMS_ZONES.TITLE, String.Empty);
            cMSZonesInfo.KeyZone = SqlClientUtility.GetString(dataReader, DbConstants.CMS_ZONES.KEYZONE, String.Empty);
            cMSZonesInfo.CheckZone = SqlClientUtility.GetInt32(dataReader, DbConstants.CMS_ZONES.CHECKZONE, 0);
            cMSZonesInfo.TrangHienThi = SqlClientUtility.GetString(dataReader, DbConstants.CMS_ZONES.TRANGHIENTHI, string.Empty);
            cMSZonesInfo.ZoneUrl = SqlClientUtility.GetString(dataReader, DbConstants.CMS_ZONES.ZONEURL, string.Empty);

            return cMSZonesInfo;
        }

        public int GetZoneId(string zoneUrl)
        {            
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@zoneUrl",zoneUrl)
            }
            ;
            using (DataTable dt = SqlClientUtility.ExecuteDataTable(connectionStringName, CommandType.StoredProcedure, "CMS_Zones_getZoneId", parameters))
            {
                if (dt.Rows.Count > 0)
                {
                    return Convert.ToInt32(dt.Rows[0]["ZoneId"]);
                }
                else
                {
                    return 0;
                }
            }
        }

        public CHRTList<CMSZonesInfo> GetZoneId(int zoneId, int lang)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@ZoneId", zoneId),
                new SqlParameter("@Lang", lang)                
			};
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CMS_Zones_listZoneId", parameters))
            {
                CHRTList<CMSZonesInfo> cMSZonesInfoList = new CHRTList<CMSZonesInfo>();
                while (dataReader.Read())
                {
                    CMSZonesInfo cMSZonesInfo = GetCMSZonesInfo(dataReader);
                    cMSZonesInfoList.Add(cMSZonesInfo);
                }

                return cMSZonesInfoList;
            }
        }

        public CHRTList<CMSZonesInfo> GetZoneIdFull(int zoneId, int lang)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@ZoneId", zoneId),
                new SqlParameter("@Lang", lang)                
			};
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CMS_Zones_listZoneIdFull", parameters))
            {
                CHRTList<CMSZonesInfo> cMSZonesInfoList = new CHRTList<CMSZonesInfo>();
                while (dataReader.Read())
                {
                    CMSZonesInfo cMSZonesInfo = GetCMSZonesInfo(dataReader);
                    cMSZonesInfoList.Add(cMSZonesInfo);
                }

                return cMSZonesInfoList;
            }
        }       
		#endregion       
	}
}
